package com.mk.admin.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.mk.admin.mapper.InternetMedicalMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.DictionaryDto;
import com.mk.admin.model.entity.InternetMedicalCounsellingEntity;
import com.mk.admin.model.entity.InternetMedicalEntity;
import com.mk.admin.model.entity.InternetMedicalPrescriptionEntity;
import com.mk.admin.model.entity.InternetMedicalReturnVisitEntity;
import com.mk.admin.model.vo.InternetMedicalVo;
import com.mk.admin.service.TaskGetAccessTokenService;
import com.mk.admin.util.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TaskGetAccessTokenImpl implements TaskGetAccessTokenService {
    @Autowired
    private RedisTemplate redisTemplate;
    public static InternetMedicalEntity internetMedicalEntity = null;
    @Autowired
    private InternetMedicalMapper internetMedicalMapper;

    @Override
    public ResultDto fresh(DictionaryDto dto) {
        String typeName = dto.getRemark();
        JSONObject entries = JSONUtil.parseObj(typeName);
        String result = HttpRequest.post(
                        entries.get("url").toString())
                .header("Content-Type", "application/json") //头信息，多个头信息多次调用此方法即可
                .body(entries.toString())
                .execute().body();
        JSONObject entries1 = JSONUtil.parseObj(result);
        com.alibaba.fastjson2.JSONObject data = com.alibaba.fastjson2.JSONObject.parseObject((String) entries1.get("data"));
        System.out.println("Token" + data.get("accessToken"));
        internetMedicalEntity = new InternetMedicalEntity();
        internetMedicalEntity.setHost(entries.get("host").toString());
        internetMedicalEntity.setAccessToken(data.get("accessToken") + "");
        internetMedicalEntity.setAppSecret(entries.get("appsecret").toString());
        internetMedicalEntity.setClientId(entries.get("clientId").toString());
        redisTemplate.opsForSet().add("TaskGetAccessToken", data.get("accessToken"));
        return null;
    }

    @Override
    public ResultDto all(InternetMedicalVo vo) {
        InternetMedicalVo vo1 = getDate(vo, 0);
        ResultDto counselling = counselling(vo1);
        ResultDto resultDto = returnVisit(vo1);
        ResultDto prescription = prescription(vo1);
        return ResultDto.data(counselling.getResult() + "--" + resultDto.getResult() + "--" + prescription.getResult());
    }

    /**
     * 网络咨询（网络门诊）服务
     *
     * @param vo
     * @return
     */
    @Override
    public ResultDto counselling(InternetMedicalVo vo) {
        InternetMedicalVo vo1 = getDate(vo, -1);
        internetMedicalEntity.setApi("/wjw/upload/uploadConsult");
        List<InternetMedicalCounsellingEntity> list = internetMedicalMapper.counselling(vo1);
        com.alibaba.fastjson2.JSONObject jsonObject = httpRequest(com.alibaba.fastjson2.JSONObject.toJSONString(list));
        return ResultDto.data(jsonObject);
    }

    /**
     * 网络复诊服务
     *
     * @param vo
     * @return
     */
    @Override
    public ResultDto returnVisit(InternetMedicalVo vo) {
        InternetMedicalVo vo1 = getDate(vo, -1);
        internetMedicalEntity.setApi("/wjw/upload/uploadFurtherConsult");
        List<InternetMedicalReturnVisitEntity> list = internetMedicalMapper.returnVisit(vo1);

        com.alibaba.fastjson2.JSONObject jsonObject = httpRequest(com.alibaba.fastjson2.JSONObject.toJSONString(list));
        return ResultDto.data(jsonObject);
    }

    /**
     * 电子处方服务
     *
     * @param vo
     * @return
     */
    @Override
    public ResultDto prescription(InternetMedicalVo vo) {
        InternetMedicalVo vo1 = getDate(vo, -1);
        internetMedicalEntity.setApi("/wjw/upload/uploadRecipe");
        List<InternetMedicalPrescriptionEntity> list = internetMedicalMapper.prescription(vo1);

        return ResultDto.data(list);
//        com.alibaba.fastjson2.JSONObject jsonObject = httpRequest(com.alibaba.fastjson2.JSONObject.toJSONString(list));
//        return ResultDto.data(jsonObject);
    }

    private com.alibaba.fastjson2.JSONObject httpRequest(String body) {
        String url = internetMedicalEntity.getHost() + internetMedicalEntity.getApi();
        String result = HttpRequest.post(url)
                .header("Content-Type", "application/json") //头信息，多个头信息多次调用此方法即可
                .body(body)
                .execute().body();
        return com.alibaba.fastjson2.JSONObject.parseObject(result);
    }

    private InternetMedicalVo getDate(InternetMedicalVo vo, int day) {
        if (vo.getBeginDate() == null) {
            vo.setBeginDate(DateTool.getDayZero(day));
            vo.setEndDate(DateTool.getDayLast(day));
        }
        vo.setAccessToken(internetMedicalEntity.getAccessToken());
        vo.setClientId(internetMedicalEntity.getClientId());
        return vo;
    }
}
